Passed
Pull Request — develop (#758)
by Kevin Van
10:04 queued 05:43
created

MatchTeaser.tsx ➔ getData   C

Complexity

Conditions 10

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 5.9999
c 0
b 0
f 0
cc 10

How to fix   Complexity   

Complexity

Complex classes like MatchTeaser.tsx ➔ getData often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import axios from "axios"
2
import classNames from "classnames"
3
import { graphql, useStaticQuery } from "gatsby"
4
import moment from "moment-timezone"
5
import "moment/dist/locale/nl-be"
6
import React, { useEffect, useState } from "react"
7
import LazyLoad from "react-lazyload"
8
9
import { Match, MatchesQueryData } from "../Types/Match"
10
import { MatchTeaserDetailProps, MatchTeaserProps } from "../Types/MatchTeaser"
11
import { mapPsdStatus } from "../scripts/helper"
12
import "./MatchTeaser.scss"
13
14
export const MatchTeaserDetail = ({ match, includeRankings }: MatchTeaserDetailProps) => {
15
  moment.tz.setDefault(`Europe/Brussels`)
16
  moment.locale(`nl-be`)
17
  moment.localeData(`nl-be`)
18
19
  console.log(moment.locales())
20
21
  const d = moment(match.date)
22
  const matchPlayed =
23
    ((match.status === 0 || match.status === null) && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) ||
24
    false
25
26
  return (
27
    <article className="match__teaser">
28
      <header>
29
        <h3>{match.teamName.replace(`Voetbal : `, ``)}</h3>
30
        {match.status !== 0 && (
31
          <div className="match__teaser__datetime__wrapper match__teaser__datetime__wrapper--status">
32
            <time
33
              className="match__teaser__datetime match__teaser__datetime--date"
34
              dateTime={d.format(`YYYY-MM-DD - H:mm`)}
35
            >
36
              {d.format(`dddd DD MMMM - H:mm`)}
37
            </time>
38
            <span className="match__teaser__datetime match__teaser__datetime--status">
39
              {mapPsdStatus(match.status)}
40
            </span>
41
          </div>
42
        )}
43
        {(match.status === 0 || match.status === null) && (
44
          <div className="match__teaser__datetime__wrapper">
45
            <time className="match__teaser__datetime match__teaser__datetime--date" dateTime={d.format(`YYYY-MM-DD`)}>
46
              {d.format(`dddd DD MMMM`)}
47
            </time>
48
            {` `}-{` `}
49
            <time className="match__teaser__datetime match__teaser__datetime--time" dateTime={d.format(`H:mm`)}>
50
              {d.format(`H:mm`)}
51
            </time>
52
          </div>
53
        )}
54
      </header>
55
      <main>
56
        <div
57
          className={classNames(`match__teaser__team`, `match__teaser__team--home`, {
58
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
59
          })}
60
        >
61
          <LazyLoad debounce={false}>
62
            <img
63
              src={match.homeClub?.logo}
64
              alt={match.homeClub?.abbreviation}
65
              className="match__teaser__logo match__teaser__logo--home"
66
            />
67
          </LazyLoad>
68
          {match.homeClub?.abbreviation || match.homeClub?.name}
69
        </div>
70
71
        {matchPlayed || <span className="match__teaser__vs">vs</span>}
72
        {matchPlayed && (
73
          <div className="match__teaser__vs match__teaser__vs--score">
74
            <div className="match__teaser__vs--score--home">{match.goalsHomeTeam}</div>-
75
            <div className="match__teaser__vs--score--away">{match.goalsAwayTeam}</div>
76
          </div>
77
        )}
78
79
        <div
80
          className={classNames(`match__teaser__team`, `match__teaser__team--away`, {
81
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
82
          })}
83
        >
84
          <LazyLoad debounce={false}>
85
            <img
86
              src={match.awayClub?.logo}
87
              alt={match.awayClub?.abbreviation}
88
              className="match__teaser__logo match__teaser__logo--away"
89
            />
90
          </LazyLoad>
91
          {match.awayClub?.abbreviation || match.awayClub?.name}
92
        </div>
93
      </main>
94
      {/* {includeRankings && match.competitionType === `Competitie` && (
95
        <MiniRanking
96
          teamId={match.homeTeamId || match.awayTeamId}
97
          homeTeam={match.homeClub?.name}
98
          awayTeam={match.awayClub?.name}
99
        />
100
      )} */}
101
    </article>
102
  )
103
}
104
105
export const MatchTeaser = ({ teamId, action, includeRankings }: MatchTeaserProps) => {
106
  if (action !== `prev` && action !== `next`) {
107
    throw new Error(`Invalid action provided`)
108
  }
109
110
  const [data, setData] = useState<Match[]>([])
111
112
  const {
113
    site: {
114
      siteMetadata: { kcvvPsdApi },
115
    },
116
  }: MatchesQueryData = useStaticQuery(graphql`
117
    {
118
      site {
119
        siteMetadata {
120
          kcvvPsdApi
121
        }
122
      }
123
    }
124
  `)
125
126
  useEffect(() => {
127
    async function getData() {
128
      const response = await axios.get(`${kcvvPsdApi}/matches/${action}`, {
129
        params: { include: teamId },
130
      })
131
      setData(response.data)
132
    }
133
    getData()
134
  }, [action, kcvvPsdApi, teamId])
135
136
  if (data.length > 0) {
137
    console.log(data[0])
138
    return <MatchTeaserDetail match={data[0]} includeRankings={includeRankings} />
139
  } else {
140
    return <div className="match__teaser__no_match">Geen wedstrijd gevonden</div>
141
  }
142
}
143
144
MatchTeaser.defaultProps = { includeRankings: false }
145
MatchTeaserDetail.defaultProps = { includeRankings: false }
146